package cn.edu.nju.ws.sview.wyge.relations.index;

import java.util.*;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.util.Version;

public class StringSplitter {
	private static StringSplitter split = null;
	public final String delimit = " \t\n\r\f~!@#$%^&*()_+|`-=\\{}[]:\";'<>?,./'";

	public static StringSplitter getStringSplitter() {
		if (split == null)
			split = new StringSplitter();
		return split;
	}

	public List<String> splitByDelimit(String s) {
		StringTokenizer tokens = new StringTokenizer(s, delimit);
		List<String> list = new LinkedList<String>();
		while (tokens.hasMoreTokens()) {
			String str = tokens.nextToken();
			int i = 0, j = 0;
			while (i < str.length() - 1) {//驼峰分词
				char cur = str.charAt(i);
				char next = str.charAt(i + 1);
				if (Character.isLowerCase(cur) && Character.isUpperCase(next)) {
					String tmp = str.substring(j, i + 1);
					if (!allUpperCase(tmp)) {
						tmp = tmp.toLowerCase();
					}
					list.add(tmp);
					j = i + 1;
				} else if (Character.isUpperCase(cur)
						&& Character.isLowerCase(next)) {
					String tmp = str.substring(j, i);
					if (tmp.length() != 0) {
						if (!allUpperCase(tmp)) {
							tmp = tmp.toLowerCase();
						}
						list.add(tmp);
						j = i;
					}
				}
				//for chinese characters  一元分词
				if(cur>=0x4e00 && cur<=0x9fa5){//if is chinese character
					//add substr before current character
					String tmp = str.substring(j, i);
					if(tmp.length() > 0){
						if(!(allUpperCase(tmp))){
							tmp = tmp.toLowerCase();
						}
						list.add(tmp);
					}
					//add current charater
					tmp = str.substring(i, i+1);
					list.add(tmp);
					//set j
					j = i+1;
				}
				
				
				//=======================
				++i;
			}

			String tmp = str.substring(j, i + 1);
			if (!allUpperCase(tmp)) {
				tmp = tmp.toLowerCase();
			}
			list.add(tmp);
		}
		return list;
	}

	private boolean allUpperCase(String part) {
		// TODO Auto-generated method stub
		for (int i = 0; i < part.length(); i++) {
			if (!Character.isUpperCase(part.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	public static void main(String args[]) {
		StringSplitter split = StringSplitter.getStringSplitter();
		System.out.println(split.splitByDelimit("chinEse中文数in据集1fff"));//"hasOBONamespace a__b  ss    "));//
	}
}
